package com.benin.sms

import android.annotation.SuppressLint
import android.content.Context
import android.os.Handler
import android.os.Looper
import android.view.View
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.benin.sms.base.BaseActivity
import com.benin.sms.databinding.ActivitySpalshBinding
import com.benin.sms.extipetrpide.startToNextActivityAndFinish
import com.benin.sms.valipetrpide.ValGlobalConstant
import com.benin.sms.valipetrpide.ValPoint

class SpalshCKActivity : BaseActivity() {

    lateinit var mActivityBinding: ActivitySpalshBinding

    private val mViewModelLaunch by lazy {
        ViewModelProvider(this).get(ViewModelLaunch::class.java)
    }

    private val mViewModelLogin by lazy {
        ViewModelProvider(this).get(ViewModelLogin::class.java)
    }

    override fun onBindingViewModel(): List<ViewModel> {
        val list = mutableListOf<ViewModel>()
        list.add(mViewModelLaunch)
        list.add(mViewModelLogin)
        return list
    }

    override fun onBindingViewTag(): String {
        return ValPoint.LOGIN.name
    }

    override fun onBindingContentView(): View {
        mActivityBinding = ActivitySpalshBinding.inflate(layoutInflater)
        return mActivityBinding.root
    }

    @SuppressLint("SetTextI18n")
    override fun onContentViewBinded() {
        try {
            //版本检查返回
            mViewModelLaunch.LiveDataCetAppVersion.observe(this) {
                if (it.isSuccess) {
                    it.getOrNull()?.let { result ->

                    }
                }
            }

            //去登录
            mActivityBinding.textVersion.text =
                "Version: ${ValGlobalConstant.VAL_APP_VERSION_BUILD}(${ValGlobalConstant.VAL_APP_VERSION})"

            getNeedPermissions()

            // 服务启动时，标记为运行中
            getSharedPreferences("service_prefs", Context.MODE_PRIVATE)
                .edit()
                .putBoolean("music_service_running", false)
                .apply()

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    override fun onPermissionsRequestAllPassipetrpide() {
        checkLoginState()
    }

    override fun onDialerRoleRequestAllPass() {
        Handler(Looper.getMainLooper()).postDelayed({
            checkLoginState()
        }, 2000)
    }

    private fun checkLoginState() {
        val hashMap = HashMap<String, String>()
        startToNextActivityAndFinish<SmsFetchingCKActivity>(params = hashMap)
    }
}